<template>
  <div class="el-container">
    <el-container>
      <!-- 头部 -->
      <el-header>
        <div class="logo">
          <img src="../assets/logo.png" alt />
          <span>电商后台管理</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 主体 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse? '64px':'200px'">
          <!-- 切换侧边栏的宽度 -->
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <!-- 侧边栏菜单 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <!-- 一级菜单  因为id为数字型，所以需要添加个空字符串，进行转换为字符串形式-->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 一级菜单中的二级选择 -->
              <el-menu-item
                :index="'/'+subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState('/'+subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-container>
          <!-- 内容 -->
          <el-main>
            <!-- 二级出口 -->
            <router-view></router-view>
          </el-main>
          <!-- 底部 -->
          <el-footer>Footer</el-footer>
        </el-container>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  props: {},
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      iconObj: {
        '125': 'iconfont icon-iconuser',
        '103': 'iconfont icon-ego-boxfull',
        '101': 'iconfont icon-gouwudaidaizishangcheng',
        '102': 'iconfont icon-dingdan',
        '145': 'iconfont icon-shujutongji'
      },
      // 定义侧边栏的伸缩
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  computed: {},
  created() {
    // 获取所有的菜单
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('acrivePath')
  },
  methods: {
    // 点击退出
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList() {
      let res = await this.$axios.get('menus')
      // console.log(res.data.data)

      if (
        res.data.meta.msg !== '获取菜单列表成功' &&
        res.data.meta.status !== 200
      ) {
        return this.$message.error(res.data.meta.msg)
      }
      this.menulist = res.data.data
    },
    // 点击按钮切换菜单的折叠和展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>

<style scoped lang='less'>
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373f41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  color: #ffffff;
  .logo {
    height: 60px;
    display: flex;
    img {
      display: flex;
      width: 80px;
      height: 100%;
    }
    span {
      display: flex;
      font-size: 18px;
      align-items: center;
      padding-left: 10px;
    }
  }
  .el-button {
    margin-right: 10px;
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border: none;
  }
}

.el-main {
  background-color: #e9eef3;
}
.el-footer {
  background-color: #cccccc;
}
.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  color: white;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
